﻿/***********************************************************
Copyright © 2015 Howyoung.
************************************************************
You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that
you conspicuously and appropriately publish on each copy an
appropriate copyright notice and disclaimer of warranty.
************************************************************
您可以对所收受的本程序源代码，无论以何种媒介，复制与发布其完
整的复制物，然而您必须以显著及适当的方式在每一份复制物上发布
适当的著作权标示及无担保声明。
***********************************************************/
using System;
using System.ComponentModel;
using System.Deployment.Application;
using System.Windows.Forms;

namespace MinecraftSavesBackup
{
    public partial class Update : Form
    {
        int pictureCount = 1;

        public Update()
        {
            InitializeComponent();
        }

        private void Update_Load(object sender, EventArgs e)
        {
            try
            {
                UpdateCheckInfo updateCheckInfo = ApplicationDeployment.CurrentDeployment.CheckForDetailedUpdate();
                decimal updateSizeMB = Math.Round(Convert.ToDecimal(updateCheckInfo.UpdateSizeBytes) / 1024 / 1024, 2);
                versionLabel.Text = "版本号: " + Application.ProductVersion + " -> " + updateCheckInfo.AvailableVersion;
                sizeLabel.Text = "大小: " + Convert.ToString(updateSizeMB) + " MB";
            }
            catch (Exception ex)
            {
                HandlingErrors.ErrorMessage(ex, "请再试一次。");
            }
        }

        private void updateButton_Click(object sender, EventArgs e)
        {
            try
            {
                panel1.Visible = false;
                panel2.Visible = true;
                ApplicationDeployment applicationDeployment = ApplicationDeployment.CurrentDeployment;
                applicationDeployment.UpdateCompleted += new AsyncCompletedEventHandler(applicationDeployment_UpdateCompleted);
                applicationDeployment.UpdateProgressChanged += new DeploymentProgressChangedEventHandler(applicationDeployment_UpdateProgressChanged);
                applicationDeployment.UpdateAsync();
            }
            catch (Exception ex)
            {
                HandlingErrors.ErrorMessage(ex, "请再试一次。");
            }
        }

        private void applicationDeployment_UpdateProgressChanged(object sender, DeploymentProgressChangedEventArgs e)
        {
            decimal MBytesCompleted = Math.Round(Convert.ToDecimal(e.BytesCompleted) / 1024 / 1024, 2);
            decimal MBytesTotal = Math.Round(Convert.ToDecimal(e.BytesTotal) / 1024 / 1024, 2);
            progressLabel.Text = "已下载 " + Convert.ToString(MBytesCompleted) + "/" + Convert.ToString(MBytesTotal) + " MB - 已完成 " + Convert.ToString(e.ProgressPercentage) + "%";
            progressBar1.Value = e.ProgressPercentage;
        }

        private void applicationDeployment_UpdateCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                MessageBox.Show("下载过程被取消。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Close();
            }
            else if (e.Error != null)
            {
                HandlingErrors.ErrorMessage(e.Error, "尝试重新下载。");
                Close();
            }
            else
            {
                if (MessageBox.Show("更新已完成，新特性将在重新启动程序后生效，要帮您重新启动程序吗？", "更新已完成", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    Properties.Settings.Default.Updated = false;
                    Properties.Settings.Default.Save();
                    Application.Restart();
                    Application.ExitThread();
                }
                else
                {
                    Properties.Settings.Default.Updated = true;
                    Properties.Settings.Default.Save();
                    Close();
                }
            }
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            ApplicationDeployment.CurrentDeployment.UpdateAsyncCancel();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            try
            {
                switch (pictureCount)
                {
                    case 1:
                        pictureBox1.Image = Properties.Resources.p1;
                        break;
                    case 2:
                        pictureBox1.Image = Properties.Resources.p2;
                        break;
                    case 3:
                        pictureBox1.Image = Properties.Resources.p3;
                        break;
                    case 4:
                        pictureBox1.Image = Properties.Resources.p4;
                        break;
                    case 5:
                        pictureBox1.Image = Properties.Resources.p5;
                        break;
                    case 6:
                        pictureBox1.Image = Properties.Resources.p6;
                        break;
                    case 7:
                        pictureBox1.Image = Properties.Resources.p7;
                        break;
                    case 8:
                        pictureBox1.Image = Properties.Resources.p8;
                        break;
                    case 9:
                        pictureBox1.Image = Properties.Resources.p9;
                        break;
                    case 10:
                        pictureBox1.Image = Properties.Resources.p10;
                        break;
                    case 11:
                        pictureBox1.Image = Properties.Resources.p11;
                        break;
                    case 12:
                        pictureBox1.Image = Properties.Resources.p12;
                        break;
                    case 13:
                        pictureBox1.Image = Properties.Resources.p13;
                        break;
                    case 14:
                        pictureBox1.Image = Properties.Resources.p14;
                        break;
                }
                pictureCount++;
                if (pictureCount > 14)
                {
                    pictureBox1.Image = Properties.Resources.p1;
                    pictureCount = 1;
                }
                pictureBox2.Image = pictureBox1.Image;
            }
            catch (Exception)
            { }
        }
    }
}
